<?php
namespace frontend\controllers;
use Yii;
use yii\web\Controller;
use yii\data\Pagination;
require '../models/sphinx/sphinxApi.php';

class SearchController extends Controller
{
    public function actionAll()
    {
        $pageSize=20;
        $pagination=new Pagination([
            "PageSize" => $pageSize,
            "totalCount"=>1000
        ]);
        
        $q=Yii::$app->request->get('q');
        $i=intval(Yii::$app->request->get('i'));
        if ($i>=1&&$i<=3){
            $index='s'.$i;
        }
        else{
            $index='*';
        }
        $cl = new \SphinxClient();
        $cl->SetServer('127.0.0.1', 9312);
        $cl->SetConnectTimeout( 3 );
        $cl->SetArrayResult(true);
        $cl->SetLimits($pagination->offset, $pagination->limit);
        $cl->SetMatchMode(SPH_MATCH_ANY);
        $res = $cl->Query(''.$q, $index);
        $pagination->totalCount=$res['total_found'];
        return $this->render('searchResult',[
            'searchStr'=>$q,
            'res'=>$res,
            'pagination'=>$pagination
        ]);
    }
}